package kk.servlet;

import com.google.gson.Gson;
import kk.domain.Book;
import kk.domain.Car;
import kk.domain.CarItem;
import kk.service.BookService;
import kk.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/carServlet")
public class CarServlet extends BaseServlet {
    private BookService bookService=new BookServiceImpl();

    protected void addItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Book book = bookService.queryBookById(id);
        //把book转换成CarItem
        CarItem carItem=new CarItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        Car car = (Car) request.getSession().getAttribute("car");
        if (car==null){
            //没有购物车
            car=new Car();
            request.getSession().setAttribute("car",car);
        }
        car.addItem(carItem);
//        System.out.println(car);
        request.getSession().setAttribute("lastName",carItem.getName());
        response.sendRedirect(request.getHeader("Referer"));

    }

    protected void deleteItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        //获取购物车
        Car car = (Car) request.getSession().getAttribute("car");
        if (car!=null){
            car.deleteItem(id);
            response.sendRedirect(request.getHeader("Referer"));
        }
    }

    protected void clearItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取购物车
        Car car = (Car) request.getSession().getAttribute("car");
        if (car!=null){
            car.clear();
            response.sendRedirect(request.getHeader("Referer"));
        }
    }

    protected void updateCount(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        int count =Integer.parseInt(request.getParameter("count"));
        Car car = (Car) request.getSession().getAttribute("car");
        if (car!=null){
            car.updateCount(id,count);
        }
        response.sendRedirect(request.getHeader("Referer"));
    }

    protected void ajaxAddItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Book book = bookService.queryBookById(id);
        //把book转换成CarItem
        CarItem carItem=new CarItem(book.getId(),book.getName(),1,book.getPrice(),book.getPrice());
        Car car = (Car) request.getSession().getAttribute("car");
        if (car==null){
            //没有购物车
            car=new Car();
            request.getSession().setAttribute("car",car);
        }
        car.addItem(carItem);
        //返回购物车总的商品数量和最后一个添加的商品名称
        Map<String,Object> resultMap = new HashMap<String,Object>();
        resultMap.put("totalCount", car.getTotalCount());
        resultMap.put("lastName",carItem.getName());
        Gson gson = new Gson();
        String resultMapJsonString = gson.toJson(resultMap);
        request.getSession().setAttribute("lastName",carItem.getName());
        response.getWriter().write(resultMapJsonString);
    }
}
